<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
/**
 * 汽车实时位置
 *
 * @icon fa fa-circle-o
 */
class CarPosition extends Backend
{

    /**
     * CarPosition模型对象
     * @var \app\admin\model\CarPosition
     */
    protected $model = null;
    const x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    // π
    const pi = 3.1415926535897932384626;
    // 长半轴
    const a = 6378245.0;
    // 扁率
    const ee = 0.00669342162296594323;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\CarPosition;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['car'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id','IMEI','CSQ','GPS_LNG','GPS_LAT','SPEED','SYS_VER','TEMP','HUM','TIME','DTIME']);
                $row->visible(['car']);
				$row->getRelation('car')->visible(['license_plate']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    public function info($ids,$pinyin)
    {
        $this->assign('id',$ids);
        $this->assign('pinyin',$pinyin);
        return $this->view->fetch();
    }

    public function user($lng,$lat)
    {
        $data = [];
        $info = self::wgs84ToBd09($lng,$lat);
        $data['lng'] = $info[0];
        $data['lat'] = $info[1];
        $this->assign('info',$data);
        $this->view->engine->layout(false);
        return $this->view->fetch();
    }

    public function position()
    {
        $params = input('param.');
        $table_name = strtolower('car_'.$params['pinyin']);
        $data = [];
        $first = [];
        $list = Db::name($table_name)->field('GPS_LNG,GPS_LAT')->where(['DTIME'=>$params['time']])->select();
        if($list)
        {
            foreach ($list as $k=>$v)
            {

                $info = self::wgs84ToBd09($v['GPS_LNG'],$v['GPS_LAT']);

                if($k==0)
                {
                    $first['lng'] = $info[0];
                    $first['lat'] = $info[1];
                }
                $arr = [];
                $arr['lng'] = $info[0];
                $arr['lat'] = $info[1];

                $data[] = $arr;
            }
        }

        $this->assign('first',$first);
        $this->assign('data',json_encode($data));
        $this->view->engine->layout(false);
        return $this->view->fetch();
    }

    public function position13()
    {
        $params = input('param.');
        $table_name = strtolower('car_'.$params['pinyin']);
        $data = [];
        $first = [];
        $list = Db::name($table_name)->field('GPS_LNG,GPS_LAT')->where(['DTIME'=>$params['time']])->select();
        if($list)
        {
            foreach ($list as $k=>$v)
            {

                $info = self::wgs84ToBd09($v['GPS_LNG'],$v['GPS_LAT']);
                /*dump($v);
                dump($info);exit;*/
                if($k==0)
                {
                    $first['lng'] = $info[0];
                    $first['lat'] = $info[1];
                }
                $arr = [];
                $arr['lng'] = $info[0];
                $arr['lat'] = $info[1];

                $data[] = $arr;
            }
        }

        $this->assign('first',$first);
        $this->assign('data',json_encode($data));
        $this->view->engine->layout(false);
        return $this->view->fetch();
    }

    public static function wgs84ToBd09 (float $lng, float $lat): array {
        $gcj = self::wgs84ToGcj02($lng, $lat);
        return self::gcj02ToBd09($gcj[0], $gcj[1]);
    }

    public static function wgs84ToGcj02 (float $lng, float $lat): array {
        $d_lat = self::transformlat($lng - 105.0, $lat - 35.0);
        $d_lng = self::transformlng($lng - 105.0, $lat - 35.0);
        $rad_lat = $lat / 180.0 * self::pi;
        $magic = sin($rad_lat);
        $magic = 1 - self::ee * $magic * $magic;
        $sqrt_magic = sqrt($magic);
        $d_lat = ($d_lat * 180.0) / ((self::a * (1 - self::ee)) / ($magic * $sqrt_magic) * self::pi);
        $d_lng = ($d_lng * 180.0) / (self::a / $sqrt_magic * cos($rad_lat) * self::pi);
        $mg_lat = $lat + $d_lat;
        $mg_lng = $lng + $d_lng;
        return [$mg_lng, $mg_lat];
    }

    /**
     * 火星坐标系(GCJ-02)转百度坐标系(BD-09)
     *
     * @param float $lng 火星坐标经度
     * @param float $lat 火星坐标纬度
     * @return array 百度坐标数组
     * @see 谷歌、高德——>百度
     */
    public static function gcj02ToBd09 (float $lng, float $lat): array {
        $z = sqrt($lng * $lng + $lat * $lat) + 0.00002 * sin($lat * self::x_pi);
        $theta = atan2($lat, $lng) + 0.000003 * cos($lng * self::x_pi);
        $bd_lng = $z * cos($theta) + 0.0065;
        $bd_lat = $z * sin($theta) + 0.006;
        return [$bd_lng, $bd_lat];
    }

    /**
     * 纬度转换
     * @param float $lng
     * @param float $lat
     * @return float|int
     */
    public static function transFormLat (float $lng, float $lat): float {
        $ret = -100.0 + 2.0 * $lng + 3.0 * $lat + 0.2 * $lat * $lat + 0.1 * $lng * $lat + 0.2 * sqrt(abs($lng));
        $ret += (20.0 * sin(6.0 * $lng * self::pi) + 20.0 * sin(2.0 * $lng * self::pi)) * 2.0 / 3.0;
        $ret += (20.0 * sin($lat * self::pi) + 40.0 * sin($lat / 3.0 * self::pi)) * 2.0 / 3.0;
        $ret += (160.0 * sin($lat / 12.0 * self::pi) + 320 * sin($lat * self::pi / 30.0)) * 2.0 / 3.0;
        return $ret;
    }

    /**
     * 经度转换
     * @param float $lng
     * @param float $lat
     * @return float
     */
    public static function transFormLng (float $lng, float $lat): float {
        $ret = 300.0 + $lng + 2.0 * $lat + 0.1 * $lng * $lng + 0.1 * $lng * $lat + 0.1 * sqrt(abs($lng));
        $ret += (20.0 * sin(6.0 * $lng * self::pi) + 20.0 * sin(2.0 * $lng * self::pi)) * 2.0 / 3.0;
        $ret += (20.0 * sin($lng * self::pi) + 40.0 * sin($lng / 3.0 * self::pi)) * 2.0 / 3.0;
        $ret += (150.0 * sin($lng / 12.0 * self::pi) + 300.0 * sin($lng / 30.0 * self::pi)) * 2.0 / 3.0;
        return $ret;
    }

}
